/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addFrame.java
 *
 * Created on 2011. 2. 3, pm 3:29:39
 */
/**
 *
 * @author Gi-Hoon
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 400, Short.MAX_VALUE)
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 300, Short.MAX_VALUE)
 );

 pack();
 }// </editor-fold>
 */
// Variables declaration - do not modify
// End of variables declaration
package todomanager.View;

/**
 *
 * @author Gi-Hoon
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import todomanager.data.ITask;
import todomanager.data.StandardTask;

public class AddFrameView extends JFrame implements ChangeListener,
		ActionListener {

	private ResourceBundle language;

	private JLabel lName = new JLabel();
	private JLabel lDescription = new JLabel();
	private JLabel lCategory = new JLabel();
	private JLabel lDate = new JLabel();
	private JLabel lDate_form = new JLabel();
	private JLabel lPriority = new JLabel();
	private JLabel lProgress = new JLabel();
	private JTextField textName = new JTextField();
	private JTextArea textDescription = new JTextArea();
	private JComboBox comboCategory = new JComboBox();
	private JTextField textDate = new JTextField();
	private JComboBox comboPriority = new JComboBox();
	private JComboBox comboProgress = new JComboBox();
	// private JButton buttonAddmore;
	private JButton buttonOk;
	private JButton buttonCancel;
	private JPanel panelLabel = new JPanel(new GridLayout(0, 1));
	private JPanel panelDate = new JPanel(new GridLayout(0, 1));
	private JPanel panelComponent = new JPanel(new GridLayout(0, 1));
	private JPanel panelButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));

	private TitledBorder titleName;
	private TitledBorder titleDescription;
	private TitledBorder titlePriority;
	private TitledBorder titleDate;
	private TitledBorder titleProgress;
	private TitledBorder titleCategory;

	private int valProgress;
	private JSlider Progress = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);

	public AddFrameView(ResourceBundle lang) {
		try {
			this.language = lang;
			Aframe();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void Aframe() throws Exception {
		this.setSize(new Dimension(400, 300));

		this.setLayout(new BorderLayout());
		/*
		 * lName.setText(this.getLabel("taskNameLabel")); lName.setBounds(new
		 * Rectangle(40, 15));
		 * 
		 * lDescription.setText(this.getLabel("taskDescriptionLabel"));
		 * lDescription.setBounds(new Rectangle(70, 15));
		 * 
		 * lCategory.setText(this.getLabel("taskCategoryLabel"));
		 * lCategory.setBounds(new Rectangle(65, 15));
		 * 
		 * lDate.setText(this.getLabel("taskDateLabel")); lDate.setBounds(new
		 * Rectangle(40, 15)); lDate_form.setText("(yyyy-mm-dd)");
		 * lDate_form.setBounds(new Rectangle(80,15));
		 * 
		 * lPriority.setText(this.getLabel("taskPriorityLabel"));
		 * lPriority.setBounds(new Rectangle(40, 15));
		 * 
		 * lProgress.setText("Progress"); lProgress.setBounds(new
		 * Rectangle(65,15));
		 */
		textName.setBounds(new Rectangle(200, 20));

		textDescription.setBounds(new Rectangle(200, 50));

		comboCategory.setBounds(new Rectangle(200, 20));
		comboCategory.addItem(this.getLabel("taskCategoryAssignment"));
		comboCategory.addItem(this.getLabel("taskCategoryMeeting"));
		comboCategory.addItem(this.getLabel("taskCategoryClass"));

		// TODO: get rid of below?
		comboCategory.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Combo_category_actionPerformed(e);
			}
		});

		textDate.setBounds(new Rectangle(200, 20));

		comboPriority.setBounds(new Rectangle(200, 20));
		comboPriority.addItem(this.getLabel("taskPriorityLow"));
		comboPriority.addItem(this.getLabel("taskPriorityNormal"));
		comboPriority.addItem(this.getLabel("taskPriorityHigh"));
		comboPriority.addItem(this.getLabel("taskPriorityVeryHigh"));

		textName.setText(this.getLabel("taskNameLabel"));
		textDate.setText(this.getLabel("taskDateLabel"));

		// Progress Slider

		Progress.setMajorTickSpacing(10);
		Progress.setMinorTickSpacing(5);
		Progress.setPaintTicks(true);
		Progress.setPaintLabels(true);
		Progress.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		Progress.addChangeListener(this);

		comboProgress.setBounds(new Rectangle(200, 20));
		comboProgress.addItem("0");
		comboProgress.addItem("1");
		comboProgress.addItem("2");
		comboProgress.addItem("3");
		comboProgress.addItem("4");
		comboProgress.addItem("5");
		comboProgress.addItem("6");
		comboProgress.addItem("7");
		comboProgress.addItem("8");
		comboProgress.addItem("9");
		comboProgress.addItem("10");

		// set border.
		titleName = BorderFactory.createTitledBorder(this.getLabel("taskNameLabel"));
		titleDescription = BorderFactory.createTitledBorder(this.getLabel("taskDescriptionLabel"));
		titleDate = BorderFactory.createTitledBorder(this.getLabel("taskDateLabel")+"(yyyy-mm-dd)");
		titleCategory = BorderFactory.createTitledBorder(this.getLabel("taskCategoryLabel"));
		titlePriority = BorderFactory.createTitledBorder(this.getLabel("taskPriorityLabel"));
		titleProgress = BorderFactory.createTitledBorder(this.getLabel("taskProgressLable"));

		textName.setBorder(titleName);
		textDescription.setBorder(titleDescription);
		textDate.setBorder(titleDate);
		comboCategory.setBorder(titleCategory);
		comboPriority.setBorder(titlePriority);
		Progress.setBorder(titleProgress);
		/*
		 * panelDate.add(lDate); panelDate.add(lDate_form);
		 * 
		 * 
		 * panelLabel.add(lName); panelLabel.add(lDescription);
		 * panelLabel.add(panelDate); panelLabel.add(lCategory);
		 * panelLabel.add(lPriority); panelLabel.add(lProgress);
		 */
		panelComponent.add(textName);
		panelComponent.add(textDescription);
		panelComponent.add(textDate);
		panelComponent.add(comboCategory);
		panelComponent.add(comboPriority);
		panelComponent.add(Progress);

		this.add("South", panelButton);
		// this.add("West", panelLabel);
		this.add("Center", panelComponent);
	}

	/**
	 * 
	 * @return the inputted data from the different fields in the view.
	 */
	public ITask getUserInput() {
		if (correctInput()) {
			return new StandardTask(textName.getText(),
					textDescription.getText(), textDate.getText(), 
							comboCategory.getSelectedItem().toString(),
					comboPriority.getSelectedIndex(), valProgress,
					(valProgress == 100 ? true : false));
		} else {
			String[] errorMessage = {"Wrong date format", "Use: yyyy-MM-dd"}; 
			JOptionPane errorPane = new JOptionPane();
			errorPane.setMessage(errorMessage);
			JDialog inputErrorDialog = errorPane.createDialog("Error!");
			inputErrorDialog.setVisible(true);
			
			return null;
		}
	}

	private boolean correctInput() {
		String dateString = textDate.getText();

		try {
			String date[] = dateString.split("-");
			int year = new Integer(date[0]);
			int month = new Integer(date[1]);
			int day = new Integer(date[2]);

			if (month > 0 && month <= 12 && day > 0 && day <= 31) {
				return true;
			} else {
				return false;
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			return false;
		}

	}

	public void addButtonActions(Action addTaskAction, Action cancelAction,
			Action addMoreAction) {
		this.buttonOk = new JButton(addTaskAction);
		this.buttonCancel = new JButton(cancelAction);
		// this.buttonAddmore = new JButton(addMoreAction);

		// panelButton.add(buttonAddmore);
		panelButton.add(buttonOk);
		panelButton.add(buttonCancel);
	}

	public String getLabel(String labelName) {
		return this.language.getString(labelName);
	}

	/**
	 * 
	 * @param e
	 *            ActionEvent for the combobox.
	 */
	private void Combo_category_actionPerformed(ActionEvent e) {
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		JSlider source = (JSlider) e.getSource();
		if (!source.getValueIsAdjusting()) {
			valProgress = (int) source.getValue();

		}

	}
}
